/*
 * nlsync
 * Copyright (c) 2010 apechurin 
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace nlsync.Resx
{
    /// <summary>
    /// Implementation of the IStringInfo interface for a Resx based 
    /// implementation.
    /// </summary>
    class ResxStringInfo : IStringInfo
    {
        #region Constructor

        /// <summary>
        /// Constructs a string from a specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="bundle">The parent bundle.</param>
        public ResxStringInfo(XmlElement element, ResxStringBundle bundle)
        {
            mXmlElement = element;
            mBundle = bundle;
        }

        #endregion

        #region IStringInfo methods

        public string Name
        {
            get
            {
                return mXmlElement.GetAttribute("name");
            }
        }

        // alias for Value
        public string DefaultValue
        {
            get
            {
                return Value;
            }
            set
            {
                Value = value;
            }
        }

        public string Value
        {
            get
            {
                return GetChildText("value");
            }
            set
            {
                SetChildText("value", value);
            }
        }

        public string Comment
        {
            get
            {
                return GetChildText("comment");
            }
            set
            {
                if (mBundle.IsCommentSupported)
                    SetChildText("comment", value);
            }
        }

        #endregion

        /// <summary>
        /// Gets InnerText of a specified child.
        /// </summary>
        /// <param name="childName">The child name.</param>
        /// <returns>The inner text.</returns>
        private string GetChildText(string childName)
        {
            XmlElement valueElem = mXmlElement.SelectSingleNode(childName)
                as XmlElement;
            return valueElem != null ? valueElem.InnerText : "";
        }

        /// <summary>
        /// Sets InnerText of a specified child.
        /// </summary>
        /// <param name="childName">The child name.</param>
        /// <param name="value">The value.</param>
        private void SetChildText(string childName, string value)
        {
            XmlElement valueElem = mXmlElement.SelectSingleNode(childName)
                as XmlElement;
            if (valueElem == null)
            {
                valueElem = mXmlElement.OwnerDocument.CreateElement(childName);
                mXmlElement.AppendChild(valueElem);
            }
            valueElem.InnerText = value;
            mBundle.Modified = true;
        }

        #region Internal methods

        /// <summary>
        /// Removes the string info. The object cannot be used anymore after 
        /// the call.
        /// </summary>
        internal void Remove()
        {
            mXmlElement.ParentNode.RemoveChild(mXmlElement);
            mXmlElement = null;
        }

        #endregion

        #region Data fields

        private XmlElement mXmlElement;
        private ResxStringBundle mBundle;

        #endregion
    }
}
